#!/usr/bin/perl -w

umask 002;

$cachedir='/org/lists.debian.org/archive-spam-removals/review';

die ("$0: needs to be run as user listweb\n") if ($ENV{'LOGNAME'} ne 'listweb');

open (HEADER, "/org/lists.debian.org/html/header.in") or 
  die ("$0: can't open /org/lists.debian.org/html/header.in: $!\n");
while (<HEADER>) {
  s/u_TITLE_u/Listarchive Review Statistic/;
  print;
}
close (HEADER);

print "<H2>Listarchive Review Statistic</h2>\n";
printf "<p>Last update at %s\n", scalar localtime((stat("$cachedir/cache"))[9]);
print "<hr noshade width=\"100%\" size=\"1\">\n";

my $msgid;

if (-e "$cachedir/cache") {
  open (CACHE, "$cachedir/cache") or die ("$0: can't open $cachedir/cache for reading: $!\n");
  while (<CACHE>) {
    chomp;
    ($post, $count, $msgid, $ham, $spam, $inappropriate) = split (/:/);
    $list = (split(m#\/#, $post))[0];
    $count_nominations += $count;
    $count_nominations{$list} += $count;
    $count_nominated++;
    $count_nominated{$list}++;
    if ($count >= 5) {
      $count_nominated_considered++;
      $count_nominated_considered{$list}++;
    } else {
      $count_nominated_considered{$list} = 0 unless $count_nominated_considered{$list};
    }
    @{$reviewed{'Ham'}} = split(',', $ham);
    @{$reviewed{'Spam'}} = split(',', $spam);;
    @{$reviewed{'Inappropriate'}} = split(',', $inappropriate);;
    $count_reviewed += (scalar @{$reviewed{'Ham'}} + scalar @{$reviewed{'Spam'}} + scalar @{$reviewed{'Inappropriate'}});
    $count_reviewed{$list} += (scalar @{$reviewed{'Ham'}} + scalar @{$reviewed{'Spam'}} + scalar @{$reviewed{'Inappropriate'}});
    $count_reviewed_spam += scalar @{$reviewed{'Spam'}};
    $count_reviewed_spam{$list} += scalar @{$reviewed{'Spam'}};
    $count_reviewed_ham += scalar @{$reviewed{'Ham'}};
    $count_reviewed_ham{$list} += scalar @{$reviewed{'Ham'}};
    $count_reviewed_inappropriate += scalar @{$reviewed{'Inappropriate'}};
    $count_reviewed_inappropriate{$list} += scalar @{$reviewed{'Inappropriate'}};
    if (scalar @{$reviewed{'Spam'}} >= (scalar @{$reviewed{'Ham'}}*2+scalar @{$reviewed{'Inappropriate'}}*2+3)) {
      $count_removed++;
      $count_removed{$list}++;
    } else {
      $count_removed{$list} = 0 unless $count_removed{$list};
    }
    if (scalar @{$reviewed{'Ham'}} >= (scalar @{$reviewed{'Spam'}}*2+scalar @{$reviewed{'Inappropriate'}}*2+3)) {
      $count_ham++;
      $count_ham{$list}++;
    } else {
      $count_ham{$list} = 0 unless $count_ham{$list};
    }
    if (scalar @{$reviewed{'Inappropriate'}} >= (scalar @{$reviewed{'Ham'}}*2+scalar @{$reviewed{'Spam'}}*2+3)) {
      $count_inappropriate++;
      $count_inappropriate{$list}++;
    } else {
      $count_inappropriate{$list} = 0 unless $count_inappropriate{$list};
    }
    foreach $reviewer (@{$reviewed{'Spam'}}) {
      $count_reviewer{$reviewer}++;
      $count_reviewer_spam{$reviewer}++;
    }
    foreach $reviewer (@{$reviewed{'Ham'}}) {
      $count_reviewer{$reviewer}++;
      $count_reviewer_ham{$reviewer}++;
    }
    foreach $reviewer (@{$reviewed{'Inappropriate'}}) {
      $count_reviewer{$reviewer}++;
      $count_reviewer_inappropriate{$reviewer}++;
    }
  }
}

print "<h3>per List</h3>\n";

print "<table border=\"1\">\n";

print "<tr><th>List<th>Nominations<th>nominated<br>Posts<th>considered<br>Posts<th>Reviews<th>as<br>spam<th>as<br>ham<th>as<br>inapproporiate<th>removed<br>posts<th>certified<br>ham<th>certified<br>inappropriate</tr>\n";

printf "<tr align=\"right\"><th align=\"left\">%s<td>%d<td>%d<td>%d<td>%d<td>%d<td>%d<td>%d<td>%d<td>%d<td>%d</tr>\n", 'TOTAL', $count_nominations, $count_nominated, $count_nominated_considered, $count_reviewed, $count_reviewed_spam, $count_reviewed_ham, $count_reviewed_inappropriate, $count_removed, $count_ham, $count_inappropriate;

foreach $list (sort {$count_nominated_considered{$b} <=> $count_nominated_considered{$a} ||
		     $count_nominated{$b} <=> $count_nominated{$a} ||
		     $count_nominations{$b} <=> $count_nominations{$a}} keys %count_nominations) {
  printf "<tr align=\"right\"><th align=\"left\"><a href=\"/cgi-bin/review/review2.pl?%s\">%s</a><td>%d<td>%d<td>%d<td>%d<td>%d<td>%d<td>%d<td>%d<td>%d<td>%d</tr>\n", 
	$list, 
	$list, 
	$count_nominations{$list}, 
	$count_nominated{$list}, 
	$count_nominated_considered{$list}, 
	$count_reviewed{$list}, 
	$count_reviewed_spam{$list}, 
	$count_reviewed_ham{$list}, 
	$count_reviewed_inappropriate{$list}, 
	$count_removed{$list}, 
	$count_ham{$list}, 
	$count_inappropriate{$list};
}

print "</table>\n";

print "<h3>per Reviewer</h3>\n";
print "<table border=\"1\">\n";
print "<tr><th>List<th>Reviews<th>as<br>spam<th>as<br>ham<th>as<br>inappropriate</tr>\n";

printf "<tr align=\"right\"><th align=\"left\">%s<th>%d<th>%d<th>%d<th>%d\n", 'TOTAL', $count_reviewed, $count_reviewed_spam, $count_reviewed_ham, $count_reviewed_inappropriate;
foreach $reviewer (sort {$count_reviewer{$b} <=> $count_reviewer{$a}} keys %count_reviewer) {
  printf "<tr align=\"right\"><th align=\"left\"><a name='%s'>%s</a><td>%d<td>%d<td>%d<td>%d</tr>\n", 
	$reviewer,
	$reviewer, 
	$count_reviewer{$reviewer}, 
	($count_reviewer_spam{$reviewer} ? $count_reviewer_spam{$reviewer} : 0), 
	($count_reviewer_ham{$reviewer} ? $count_reviewer_ham{$reviewer} : 0), 
	($count_reviewer_inappropriate{$reviewer} ? $count_reviewer_inappropriate{$reviewer} : 0);
}

print "</table>\n";

open (FOOTER, "/org/lists.debian.org/html/footer.in") or
  die ("$0: can't open /org/lists.debian.org/html/footer.in: $!\n");
while (<FOOTER>) {
  print;
}
close (FOOTER);
